#!/bin/bash

# 旋转指示器字符
spinner="/|\\-"

printf_app_log() {
  # 应用列表
  local apps=("${!1}")
  # 构建状态
  local build_statuses=("${!2}")
  # 部署状态
  local dev_statuses=("${!3}")
  # 启动状态
  local run_statuses=("${!4}")
  local spin_char=$5

  # 输出表格标题
  echo "--------------------------------------------------"
  printf "🔧 :进行中  ⌛ :等待开始  ❌  :失败  ✅  :成功\n"
  echo "--------------------------------------------------"

  # 循环输出应用名和状态，实现对齐显示
  for ((i = 0; i < ${#apps[@]}; i++)); do
      local app_name="${apps[$i]}"
      local build_status="${build_statuses[$i]}"
      [[ -z "$build_status" ]] && build_status="⌛"
      local dev_status="${dev_statuses[$i]}"
      [[ -z "$dev_status" ]] && dev_status="⌛"
      local run_status="${run_statuses[$i]}"
      [[ -z "$run_status" ]] && run_status="⌛"

      printf "%-20s 构建 %-5s 部署 %-5s 启动 %-5s %s\n" \
          "$app_name" "$build_status" "$dev_status" "$run_status" "$spin_char"
  done

  echo "--------------------------------------------------"
}
# 基础请求
base_request(){
   api_response=$(curl -s "$1" \
        -H 'Accept: application/json, text/plain, */*' \
        -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6' \
        -H 'Connection: keep-alive' \
        -H "Cookie: vops_token=$2" \
        -H 'Referer: https://vops.hszq8.com/app/flow/flow_detail' \
        -H 'Sec-Fetch-Dest: empty' \
        -H 'Sec-Fetch-Mode: cors' \
        -H 'Sec-Fetch-Site: same-origin' \
        -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0' \
        -H 'sec-ch-ua: "Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"' \
        -H 'sec-ch-ua-mobile: ?0' \
        -H 'sec-ch-ua-platform: "macOS"')
      echo "$api_response"
}

# 获取token
get_vops_token() {
  # 1. 调用接口获取git地址数组
  vops_result=$(curl -s https://admin-feature.vbkrhk.com/auto-ai/vops/user/effective/token)
  # 使用jq来提取data字段的值
  token=$(echo "$vops_result" | jq -r '.data')
  # 校验data是否有值
  if [ -z "$token" ]; then
    echo "响应中的data字段为空，流程终止。"
    exit 1
  fi
  echo "$token"
}

main() {
  # 1. 获取能用的token
  token=$(get_vops_token)
  # 打印获取到的token
  echo "获取 ops token: $token"

  # 初始化应用列表
  local apps=("apppp1" "apppp2" "23123213123")
  # 初始化状态，全部为空
  local build_statuses=("⌛" "⌛" "⌛")
  local dev_statuses=("⌛" "⌛" "⌛")
  local run_statuses=("⌛" "⌛" "⌛")

  local iteration=0
  local spinner_index=0

  while :; do
    # 清屏，刷新显示
    clear

    # 获取当前的旋转字符
    local spin_char="${spinner:spinner_index:1}"

    # 模拟更新状态
    for ((i = 0; i < ${#apps[@]}; i++)); do
      # 模拟随机状态更新
      if ((iteration % 2 == 0)); then
        build_statuses[$i]="🔧"
      fi
      if ((iteration % 3 == 0)); then
        dev_statuses[$i]="✅"
      fi
      if ((iteration % 5 == 0)); then
        run_statuses[$i]="✅"
      fi
    done

    # 调用打印函数
    printf_app_log apps[@] build_statuses[@] dev_statuses[@] run_statuses[@] "$spin_char"

    # 检查是否所有任务完成
    all_done=true
    for status in "${build_statuses[@]}" "${dev_statuses[@]}" "${run_statuses[@]}"; do
      if [[ "$status" == "⌛" || "$status" == "🔧" ]]; then
        all_done=false
        break
      fi
    done

    # 如果所有状态都成功，则退出
    if $all_done; then
      echo "所有任务已完成！"
      break
    fi

    # 旋转字符更新
    spinner_index=$(( (spinner_index + 1) % 4 ))

    # 等待1秒
    sleep 1

    # 模拟迭代次数
    iteration=$((iteration + 1))
  done
}

# 执行主流程
main "$@"
